<?php
require_once('MForm.php');

/**
 * API class to read Doctrine models into a form
 */
class ModelForm extends MForm implements IMForm {
    private $model;
    
    function set_model($model){
        $this->model = $model;
        
        $this->form_fields = $this->get_fields_from_model();
        
        if($this->data != null && !empty($this->data)){
            $this->validate_fields();
        }
    }
    
    function __construct($data=null){
        $this->data = $data;
    }
    
    private function get_fields_from_model(){
        #$all_vars = get_class_vars( get_class($this->model));
        #$all_vars = get_class_vars($this->model);
        
        $model = new $this->model;
        $table = $model->getTable();
        $columns = $table->getColumns();
        foreach($columns as $field_name=>$db_property){
            $label = ucwords(str_replace('_',' ',$field_name));
            
            if($field_name == 'updated_at') continue;
            if($field_name == 'created_at') continue;
            if($field_name == 'deleted_at') continue;
            if($field_name == 'modified_by') continue;
            if($field_name == 'created_by') continue;
            
            #echo "<pre>";
            #echo $field_name.'<br>';
            #print_r($db_property);
            
            $property = array();
            
            if(isset($db_property['primary']) && $db_property['primary'] == 1){
                $property['type'] = 'hidden';
            } else if(isset($db_property['type']) && $db_property['type'] == 'integer' &&
                      isset($db_property['length']) && $db_property['length'] == 1)
            {
                $property['type'] = 'checkbox';

            } else {
                $property['type'] = 'text';
            }
            $property['label'] = $label;
            
            #if(isset($db_property['notnull']) && $db_property['notnull'] == 1 && !isset($db_property['default'] )){
            if(isset($db_property['notnull']) && $db_property['notnull'] == 1 ){
                if( !isset($db_property['default']) ){
                    $property['blank'] = false;
                }
                
            }
            
            #$property['as_array'] = strtolower(get_class($model));
                        
            $all_vars[$field_name] = $property;
            $all_vars[$field_name]['error_message'] = '';
        }        
        return $all_vars;
    }
    
    public function save($saved_object=null){
        if($saved_object){
            $model = $saved_object;
        } else {
            $model = new $this->model;
        }
        
        #foreach($this->data as $var=>$value){
        #    $model->$var = $value;
        #}
        # or above $user->fromArray($user_post);
        $model->fromArray($this->data);
        try {
		    $model->save();
		    return $model;
		} catch(Exception $e){
		    $this->global_error = $e->getMessage();
		    throw new Exception($e);
		}
    }
}
